package io;

import graphics.datastructures.Model;
import graphics.datastructures.SimpleMesh;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class ObjectFileWriter {
	private SimpleMesh _mesh;
	private ObjectOutputStream _out;
	public ObjectFileWriter(String file,SimpleMesh mesh) throws FileNotFoundException, IOException
	{
		
		_out = new ObjectOutputStream(new FileOutputStream(new File(file)));
		_mesh = mesh;
	}
	void write() throws IOException
	{
		_out.writeObject(_mesh);
		_out.close();
	}
	public static void main(String[] args) {
		try {
			dostufff();
		} catch (Exception e) {
			e.printStackTrace();
		}
		try {
			dostuff2();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	private static void dostuff2() throws Exception{
		ObjectFileReader reader = new ObjectFileReader("c:\\cgobjects\\treeleaves.cache");
		long t = System.currentTimeMillis();
		Model mesh = reader.read();
		System.out.println("cache read in"+(System.currentTimeMillis()-t)/1000f);
	}
	private static void dostufff() throws Exception{
		long t = System.currentTimeMillis();
		ModelReader reader = new SimpleReader("c:/cgobjects/treeleaves.obj");
		SimpleMesh mesh = (SimpleMesh) reader.read();
		System.out.println("obj file read in"+(System.currentTimeMillis()-t)/1000f);
		ObjectFileWriter w = new ObjectFileWriter("c:\\cgobjects\\treeleaves.cache", mesh);
		w.write();
		
	}
}
